using System;
using System.Collections.Generic;
using System.Text;

namespace Guestbook.Page
{
    public class DefaultPage : BasePage
    {
        #region - Fields & Properties -

        protected System.Web.UI.WebControls.TextBox txtTitle;
        protected System.Web.UI.WebControls.TextBox txtUserName;
        protected System.Web.UI.WebControls.TextBox txtPassword;
        protected System.Web.UI.WebControls.TextBox txtUserEmail;
        protected System.Web.UI.WebControls.TextBox txtUserWebsite;
        protected System.Web.UI.WebControls.TextBox txtGuestBookId;

        protected System.Web.UI.WebControls.Button btnCreate;

        public int GuestBookId
        {
            get { return int.Parse(txtGuestBookId.Text.Trim()); }
        }

        public string Password
        {
            get { return txtPassword.Text.Trim(); }
        }

        public string UserName
        {
            get { return txtUserName.Text.Trim(); }
        }

        public string UserWebsite
        {
            get { return txtUserWebsite.Text.Trim(); }
        }

        public string UserEmail
        {
            get { return txtUserEmail.Text.Trim(); }
        }

        public string GuestBookTitle
        {
            get { return txtTitle.Text.Trim(); }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                btnCreate.Attributes.Add("onclick", "return checkRegForm();");
            }
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            //create new guestbook object
            Guestbook.domain.GuestBook newObj = new Guestbook.domain.GuestBook();

            //fill data
            newObj.Title = this.GuestBookTitle;
            newObj.UserName = this.UserName;
            newObj.UserPassword = this.Password;
            newObj.UserEmail = this.UserEmail;
            newObj.UserWebsite = this.UserWebsite;
            newObj.TotalMessages = 0;
            newObj.TotalVisitors = 1;
            newObj.Active = true;
            newObj.DateCreation = DateTime.Now;
            newObj.Code = string.Empty;

            //save if data is valid 
            if (newObj.IsValid)
            {
                newObj.Save();
                Response.Redirect(string.Format("~/guestbook.aspx?id={0}&page=1", newObj.Id));
            }
        }

        protected void btnGoto_Click(object sender, EventArgs e)
        {
            try
            {
                bool isExist
                    = Guestbook.business.service.GuestBookService.IsExist(this.GuestBookId);
                if (isExist)
                    Response.Redirect(string.Format("~/guestbook.aspx?id={0}&page=1", this.GuestBookId));
            }
            catch (Exception)
            {

            }
        }
    }
}
